package com.douqu.game.core.config.challenge;

import com.bean.core.util.Utils;
import com.douqu.game.core.config.common.CommonData;
import com.douqu.game.core.config.common.GoodsData;
import com.douqu.game.core.e.E_Attribute;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.LoadFactory;
import com.douqu.game.core.util.LoadUtils;
import com.douqu.game.core.util.LogUtils;
import com.douqu.game.core.util.time.TimeData;
import com.douqu.game.core.util.time.TimeUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;


/**
* @author wangzhenfei
*         2017-08-08 16:09
*          世界boss
*/
public class WorldBossConfig extends BattleInitConfig {

    /**
     * 头像是否显示
     */
    public int showAvatar;
    /**
     * 攻打掉落
     */
    public GoodsData[] passReward;
    /****/
    public GoodsData[] killReward;
    public GoodsData[] showReward;

    /**
     * 持续时间（分钟）
     */
    //public int duration;

    /**
     * 刷新时间（24时计算）
     */
    public transient TimeData freshTime;

    /**
     *攻防血提升倍率 攻防血
     */
    public float[] increaseRate;

    /** fc */
    public int fc;

    /**BOSS军令回复速度**/
    public CommonData[] crystalGrow;
    @Override
    public boolean check() {
        if (passReward == null) {
            LogUtils.error("WorldBossConfig check passReward is null -> id:" + id + " name:" + name);
            return false;
        }

        if (increaseRate == null || increaseRate.length != 3) {
            LogUtils.error("WorldBossConfig check increaseRate is null -> id:" + id + " name:" + name);
            return false;
        }
        if (freshTime == null) {
            LogUtils.error("WorldBossConfig check freshTime is null -> id:" + id + " name:" + name);
            return false;
        }
        if (killReward == null) {
            LogUtils.error("WorldBossConfig check killReward is null -> id:" + id + " name:" + name);
            return false;
        }

        if(crystalGrow == null)
        {
            LogUtils.error("WorldBossConfig check crystalGrow is null -> id:" + id + " name:" + name);
            return false;
        }

        if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.MAP_KEY, mapId) == null)
        {
            LogUtils.error(getClass().getSimpleName() + " Load BattleInitConfig mapId Error -> id:" + id);
            return false;
        }

        getAuctionIds();

        return super.check();
    }

    public float getExtRate(E_Attribute attribute)
    {
        if(attribute == E_Attribute.ATK)
            return increaseRate[0];

        if(attribute == E_Attribute.DEF)
            return increaseRate[1];

        if(attribute == E_Attribute.HP)
            return increaseRate[2];

        return 1;
    }



//    public boolean isCanFresh(){
//        Calendar now = Calendar.getInstance();
//        int week = now.get(Calendar.DAY_OF_WEEK);
//        if (week == 1)
//            week = 7;
//        else
//            week -= 1;
//        //如果不是同一个星期几 不可以刷新
//        if (week != freshTime.type)
//            return false;
//        int hours = now.get(Calendar.HOUR_OF_DAY);
//        int minute = now.get(Calendar.MINUTE);
//        int nowMinute = hours * 60 + minute;
//        int startTime = freshTime.id * 60 + freshTime.value;
//        if(startTime <=  nowMinute &&
//                nowMinute <  startTime + duration){
//            return true;
//        }
//        return false;
//    }


    public boolean isUnTouchTime(long startTime){
        Calendar now = Calendar.getInstance();
        return TimeUtils.isArriveTime(freshTime,now,startTime);
    }


    /**
     * 获取拍卖掉落列表
     * @return
     */
    public List<Integer> getAuctionIds(){
        List<Integer> list = new ArrayList<>();
        for(GoodsData data : killReward){
            if(Utils.checkRandom10000(data.id)){
                list.add(data.type);
            }
        }
        return list;
    }



    /**
     * 获取地图类型
     * @return
     */
    public int getMapType()
    {
        MapConfig mapConfig = DataFactory.getInstance().getGameObject(DataFactory.MAP_KEY, mapId);
        return mapConfig.mapType;
    }


    @Override
    public boolean setVariable(String key, String value) {
        if ("passReward".equals(key)) {
            passReward = LoadUtils.loadGoodDataToArray(key, value);
            return LoadUtils.checkGoods(passReward, this.getClass(), key, id);
        } else if ("killReward".equals(key)) {
            killReward = LoadUtils.loadGoodDataToArray(key, value);
            for(GoodsData data : killReward){
                if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.WORLD_AUCTION_KEY, data.type) == null){
                    LogUtils.error("Load WorldBossConfig killReward WORLD_AUCTION_KEY error! data.type:" , data.type , ",id:" , id);
                    return false;
                }
            }
            return true;
        }  else if ("freshTime".equals(key)) {
            freshTime =LoadUtils.loadTimeData(key,value);
            return true;
        } else if("increaseRate".equals(key)){
            increaseRate = LoadUtils.loadFloatArray(key, value);
            return true;
        } else if("crystalGrow".equals(key)){
            crystalGrow = LoadUtils.loadCommonDataToArray(key, value);
            return true;
        }else if("showReward".equals(key)){
            showReward = LoadUtils.loadGoodDataToArray(key, value);
            return true;
        }
        else
            return super.setVariable(key, value);
    }

    @Override
    public String toString() {
        return "WorldBossConfig{" +
                "showAvatar=" + showAvatar +
                ", passReward=" + Arrays.toString(passReward) +
                ", freshTime=" + freshTime +
                "} " + super.toString();
    }
}
